#include <iostream>
#include <memory>
#include <vector>

using std::cout;
using std::endl;
using std::unique_ptr;
using std::vector;

void test()
{
    unique_ptr<int> up(new int(10));
    cout << "*up = " << *up << endl;
    cout << "up.get() = " << up.get() << endl;

    cout << endl << endl;
    /* unique_ptr<int> up2(up);//error, 复制 */

    cout << endl;
    unique_ptr<int> up3(new int(30));
    cout << "*up3 = " << *up3 << endl;
    cout << "up3.get() = " << up3.get() << endl;

    cout << endl;
    /* up3 = up;//error, 赋值 */

    cout << endl << "移动语义" << endl;
    unique_ptr<int> up4(std::move(up));//移动构造函数
    cout << "*up4 = " << *up4 << endl;
    cout << "up4.get() = " << up4.get() << endl;

    cout << endl;
    up4 = std::move(up3);//移动赋值函数
    cout << "*up4 = " << *up4 << endl;
    cout << "up4.get() = " << up4.get() << endl;

    cout << endl << "作为容器元素" << endl;
    vector<unique_ptr<int>> vec;
    /* unique_ptr<int> tmp(new int(1000)); */
    /* vec.push_back(unique_ptr<int>(new int(1000))); */
    /* cout << "=== " <<  *vec[0] << endl; */
    /* vec.push_back(up4);//将up4拷贝到vector，而unique_ptr不能拷贝 */
    vec.push_back(std::move(up4));
    cout << "2222" << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

